{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 列表(List)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原列表 list1  [1, 2, 3, '列表1', '列表2', '列表3']\n",
      "[6, 7, 8, '列表4', '列表5']\n",
      "删除后的 list1  [1, 2, '列表1', '列表2', '列表3']\n",
      "访问list1列表中的值： [2, '列表1', '列表2']\n",
      "更新列表list1 :  [1, 2, '列表1', '列表2', '列表3', '我是新添加的元素', 'abc']\n"
     ]
    }
   ],
   "source": [
    "list1 = [1,2,3,\"列表1\",\"列表2\",'列表3']\n",
    "list2= [6,7,8,\"列表4\",'列表5']\n",
    "\n",
    "print(\"原列表 list1 \",list1)\n",
    "\n",
    "# 删除列表元素\n",
    "del list1[2]\n",
    "\n",
    "print(list2)\n",
    "print(\"删除后的 list1 \",list1)\n",
    "print(\"访问list1列表中的值：\",list1[1:4])\n",
    "\n",
    "\n",
    "# 更新列表  append 在列表末尾添加新的对象\n",
    "\n",
    "list1.append(\"我是新添加的元素\")\n",
    "list1.append('abc')\n",
    "print('更新列表list1 : ',list1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python列表脚本操作符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "长度： 7\n",
      "组合 :  [1, 2, '列表1', '列表2', '列表3', '我是新添加的元素', 'abc', 6, 7, 8, '列表4', '列表5']\n",
      "元素是否存在于列表中  2 in list1 :  True\n",
      "重复  list4 * 4 :  [6, 7, 6, 7, 6, 7, 6, 7]\n",
      " 6 == k ： True\n",
      "迭代 list4 :  6\n",
      "迭代 list4 :  7\n"
     ]
    }
   ],
   "source": [
    "#  列表对 + 和 * 的操作符与字符串相似。+ 号用于组合列表，* 号用于重复列表。\n",
    "\n",
    "list3 = list1 + list2\n",
    "list4 = [6,7]\n",
    "list5 = [9,0]\n",
    "\n",
    "print('长度：',len(list1))\n",
    "\n",
    "print('组合 : ',list3)\n",
    "\n",
    "print('元素是否存在于列表中  2 in list1 : ',2 in list1)\n",
    "\n",
    "print('重复  list4 * 4 : ',list4 * 4)\n",
    "\n",
    "for k in list4:\n",
    "    if 6 == k:\n",
    "        print(\" 6 == k ：\",(6 == k))\n",
    "    print('迭代 list4 : ',k)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python列表函数&方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "返回 list5 列表元素最大值  3\n",
      "统计9 元素在 list5 列表中出现的次数   0\n",
      "移除列表中的一个元素（默认第一个元素），并且返回该元素的值  3 移除一个元素后的list5  [3, 3]\n",
      "移除列表中某个值的第一个匹配项  None\n"
     ]
    }
   ],
   "source": [
    "# Python 3.X 的版本中已经没有 cmp 函数，如果你需要实现比较功能，需要引入 operator 模块，适合任何对象\n",
    "\n",
    "import operator\n",
    "print(operator.eq(list4,list5))\n",
    "\n",
    "print('返回 list5 列表元素最大值 ',max(list5))\n",
    "\n",
    "print('统计9 元素在 list5 列表中出现的次数  ',list5.count(9))\n",
    "\n",
    "# print('将 1 对象插入lsit5 列表 ',list5.insert(3,99),'插入新的对象后的list5 ',list5)\n",
    "\n",
    "print('移除列表中的一个元素（默认第一个元素），并且返回该元素的值 ',list5.pop(2),'移除一个元素后的list5 ',list5)\n",
    "\n",
    "print('移除列表中某个值的第一个匹配项 ',list3.remove(7));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
